// pages/putIn/putIn.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },
  shouye:function() {
    wx.switchTab({
      url: '../index/index',
      success: function (e) {
        var page = getCurrentPages().pop();
        if (page == undefined || page == null) return;
        page.onLoad();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    console.log(options)
    wx.request({
      url: app.d.ceshiUrl + '/Api/Order/order_details',
      data: {
        order_id: options.orderid,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },

      success: function (res) {
        // success
       console.log(res)
        that.setData({
          ordersn: res.data.ord.order_sn,
          orderprice: res.data.ord.price,
          sn: res.data.ord.order_sn,
          id: res.data.ord.id
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },
  //调起微信支付
  wxpay: function (e) {
    var oid = e.currentTarget.dataset.id;
    var osn = e.currentTarget.dataset.sn;
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Wxpay/wxpay',
      data: {
        order_id: oid,
        order_sn: osn,
        uid: app.d.userId,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }, // 设置请求的 header
      success: function (res) {
        if (res.data.status == 1) {
          var order = res.data.arr;
          wx.requestPayment({
            timeStamp: order.timeStamp,
            nonceStr: order.nonceStr,
            package: order.package,
            signType: 'MD5',
            paySign: order.paySign,
            success: function (res) {
              that.getcode();
              that.distribution_details_set();
            },
            fail: function (res) {
              console.log(res)
              wx.showToast({
                title: res,
                duration: 3000
              })
            }
          })
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！err:wxpay',
          duration: 2000
        });
      }
    })
  },
  distribution_details_set: function(){
      wx.request({
        url: app.d.ceshiUrl + '/Api/Wxpay/fxLogSet',
        data: {
          order_id: this.data.order_id,
          uid: app.d.userId,
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
      })
  },

  //支付成功后回调判断是否有领取体验券
  getcode: function (e) {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/User/getcode',
      method: 'post',
      data: {
        uid: that.data.userId,
        order_id: that.data.orderId,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data        
        wx.showToast({
          title: "支付成功!",
          duration: 2000,
        });
        setTimeout(function () {
          wx.navigateTo({
            url: '../user/dingdan?currentTab=1&otype=deliver',
          });
        }, 2500);
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！err:getcode',
          duration: 2000
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})